package com.bangbang.mq;

import com.bangbang.mapper.TbSeckillCourseMapper;
import com.bangbang.pojo.TbSeckillCourse;
import com.bangbang.pojo.TbSeckillCourseOrder;
import com.bangbang.service.ITbSeckillCourseOrderService;
import com.bangbang.util.IdWorker;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
@RocketMQMessageListener(topic = "course-topic",consumerGroup = "course-2012-topic")
public class SeckillMqCoumser implements RocketMQListener<Map> {

    @Autowired
    private TbSeckillCourseMapper tbSeckillCourseMapper;

    @Autowired
    private ITbSeckillCourseOrderService tbSeckillCourseOrderService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(Map map) {
        Long uid = (Long) map.get("uid");
        Long secId = (Long) map.get("secId");
//        根据当期秒杀ID查询出对应的秒杀详情数据
        TbSeckillCourse tbSeckillCourse = tbSeckillCourseMapper.selectById(secId);
//        TbSeckillGoods goods = tbSeckillGoodsMapper.selectByPrimaryKey(id.longValue());
        //        创建订单  单独取出
        IdWorker idw = new IdWorker();
        TbSeckillCourseOrder seckillOrder = new TbSeckillCourseOrder();
//        订单号
        seckillOrder.setScoId(idw.nextId());
//        订单创建是时间
        seckillOrder.setScoCreateTime(new Date());
//        商品ID
        seckillOrder.setSparA(secId.toString());
//        秒杀金额
        seckillOrder.setScoMoney(tbSeckillCourse.getSecCostPrice().toString());
//        支付状态
        seckillOrder.setScoStatus("0");
//          教师ID
        seckillOrder.setScoTeacherId(tbSeckillCourse.getSecTeacherId());
//        用户ID
        seckillOrder.setScoUserId(uid);
//        增加数据库
        tbSeckillCourseOrderService.save(seckillOrder);
//        放缓存
        redisTemplate.boundHashOps("seckill-order-id").put(uid,seckillOrder.getScoId());

    }
}
